/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.service.impl;

import cn.platform.constant.enumeration.StatusConstantEnum;
import cn.platform.dto.UserInfo;
import cn.platform.entity.User;
import cn.platform.mapper.UserMapper;
import cn.platform.page.PageFactory;
import cn.platform.service.IUserService;
import cn.platform.utils.JWTUtil;
import cn.platform.utils.RedisUtil;
import cn.platform.utils.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private RedisUtil redisUtil;
    private static final String headerToken = "Authorization";
    @Override
    public User findByUsername(String username) {
        return this.baseMapper.selectByUsername(username);
    }

    @Override
    public User findByEmail(String email) {
        return this.baseMapper.selectByEmail(email);
    }

    public User findUserInfo(Long userId){
        return this.baseMapper.selectById(userId);
    }

    @Override
    public UserInfo findUserLoginInfo(HttpServletRequest req) {
        String accessToken = req.getHeader(headerToken);
        String username = JWTUtil.getUsername(accessToken);
        log.info("===查到的用户，{}===",username);
        User user = this.baseMapper.selectByUsername(username);
        if(user == null){
            return null;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(user.getUsername());
        userInfo.setEmail(user.getEmail());
        userInfo.setStatus(user.getStatus());
        userInfo.setNickname(user.getNickname());
        userInfo.setHeadUrl(user.getHeadUrl());
        return userInfo;
    }

    @Override
    public Page<User> findAll() {
        return this.baseMapper.selectAll(PageFactory.defaultPage());
    }

    @Override
    public Boolean updateUser(User user) {
        User userOld = this.baseMapper.selectById(user.getId());
        if(userOld == null||user.getPassword().equals("")){
            return false;
        }
        if(user.getPassword()!=null){
            user.setSalt(UUID.randomUUID().toString().substring(0,5));
            user.setPassword(StringUtil.getMd5(user.getPassword()+user.getSalt()));
        }
        return this.baseMapper.updateById(user) == 1;
    }

    @Override
    public Boolean deleteUser(User user) {
        log.info("===  删除用户{}  ====",user.toString());
        User userOld = this.baseMapper.selectById(user.getId().toString());
        if(userOld == null){
            return false;
        }
        if(user.equals(userOld)){
           return this.baseMapper.updateStatusById(StatusConstantEnum.DELETED.getCode(), user.getId())==1;
        }
        return false;
    }

    @Override
    public Boolean enableUser(User user) {
        User userOld = this.baseMapper.selectById(user.getId().toString());
        if(userOld == null){
            return false;
        }
        if(user.equals(userOld)){
            return this.baseMapper.updateStatusById(StatusConstantEnum.ENABLE.getCode(), user.getId())==1;
        }
        return false;
    }

    @Override
    public Boolean insert(User user) {
        user.setSalt(UUID.randomUUID().toString().substring(0,5));
        user.setStatus(StatusConstantEnum.ENABLE.getCode());
        user.setPassword(StringUtil.getMd5(user.getPassword()+user.getSalt()));
        return this.baseMapper.insert(user) > 0;
    }
}
